package com.meichis.yndms.adapter.RecycleViewItemType;

import android.app.Activity;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.*;

import com.meichis.mcsappframework.adapter.recyclerview.base.ItemViewDelegate;
import com.meichis.mcsappframework.adapter.recyclerview.base.ViewHolder;
import com.meichis.yndms.R;
import com.meichis.yndms.model.InspectTemplate_Item;
import com.meichis.yndms.ui.activity.VST_FreeQuestionnaireLogicActivity;

import java.util.List;

public class FillMode_Switch implements ItemViewDelegate<InspectTemplate_Item> {

    private final Activity activity;
    private List<InspectTemplate_Item> items;

    public FillMode_Switch(Activity activity, List<InspectTemplate_Item> items) {
        this.items = items;
        this.activity = activity;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.fill_mode_switch;
    }

    @Override
    public boolean isForViewType(InspectTemplate_Item item, int position) {
        return item.getItem().getFillMode() == 5;
    }

    @Override
    public void convert(final ViewHolder holder, final InspectTemplate_Item item, int position) {
        TextView tv_group = holder.getView(R.id.tv_group);
        if (position == 0) {
            if (!TextUtils.isEmpty(item.getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                holder.setVisible(R.id.v_group, true);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
                holder.setVisible(R.id.v_group, false);
            }
        } else {
            if (!TextUtils.isEmpty(item.getItemGroup()) && !item.getItemGroup().equals(items.get(position - 1).getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                holder.setVisible(R.id.v_group, true);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
                holder.setVisible(R.id.v_group, false);
            }
        }

        holder.setText(R.id.tv_question, Html.fromHtml(String.valueOf(position + 1) + ". " +
                item.getItem().getName() +
                (!TextUtils.isEmpty(item.getItem().getUnit()) ? "(" + item.getItem().getUnit() + ")" : "") +
                (item.getIsMustFill() == 1 ? " <font  color=\"#FF0000\">*</font>" : "") +
                (!TextUtils.isEmpty(item.getItem().getSubTitle()) ? "<br/> <font color=\"#8D8D8D\" ><small>" + item.getItem().getSubTitle() + "</small></font>" : "")
        ));
        //当前选项是否选中
        if (item.getFillValue().trim().equals("") || item.getFillName().trim().equals("")) {
            if (TextUtils.isEmpty(item.getItem().getDefaultValue())) {
                item.setFillName("否");
                item.setFillValue("N");
            } else {
                if (item.getItem().getDefaultValue().equals("Y")) {
                    item.setFillName("是");
                    item.setFillValue("Y");
                } else {
                    item.setFillName("否");
                    item.setFillValue("N");
                }
            }
        }
        final LinearLayout ll = holder.getView(R.id.ll_answer);
        final Switch sw_answer = holder.getView(R.id.sw_answer);
        final EditText et_answer = holder.getView(R.id.et_answer);
        final TextView tv_answer = holder.getView(R.id.tv_answer);
        setAnswer(item, et_answer);
        setBackground(item, ll);
        tv_answer.setText(item.getFillName());
        sw_answer.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    item.setFillValue("Y");
                    item.setFillName("是");
                } else {
                    item.setFillValue("N");
                    item.setFillName("否");
                }
                tv_answer.setText(item.getFillName());
                setAnswer(item, et_answer);
                setBackground(item, ll);
                ((VST_FreeQuestionnaireLogicActivity)activity).notifyAdapter();
            }
        });
        sw_answer.setChecked(item.getFillValue().equals("Y"));
        et_answer.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                View nextView = v.focusSearch(View.FOCUS_DOWN);
                if (nextView != null) {
                    nextView.requestFocus(View.FOCUS_DOWN);
                }
                return true;
            }
        });
        if (!holder.haveTextChangedListener(et_answer)) {
            holder.setTextChangedListener(et_answer, new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    items.get(holder.getAdapterPosition()).setRemark(s.toString());
                    setBackground(items.get(holder.getAdapterPosition()), ll);
                }
            });
        }
    }

    private void setAnswer(InspectTemplate_Item item, EditText et_answer) {
        if (!TextUtils.isEmpty(item.getItem().getNeedExtRemark()) && item.getFillValue().equals(item.getItem().getNeedExtRemark())) {
            et_answer.setVisibility(View.GONE);
            et_answer.setText(item.getRemark());
        } else {
            item.setRemark("");
            et_answer.setVisibility(View.GONE);
            et_answer.setText(item.getRemark());
        }
    }

    private void setBackground(InspectTemplate_Item item, LinearLayout ll) {
        ll.setBackgroundResource(item.getIsMustFill() == 1 && item.getFillValue().equals(item.getItem().getNeedExtRemark()) && item.getRemark().length() == 0 ? R.drawable.bg_ll_frame : R.drawable.bg_ll_frame);
    }
}
